package com.example.shoppinglist;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{

	public static final String TITLE_KEY = "title_pref";
	public static final String COLOR_KEY = "color_pref";
	public static final String CURRENCY_KEY = "currency_pref";

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
	}

	@Override
	public void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (key.equals(TITLE_KEY)) {
			Preference titlePref = findPreference(key);
			// Set summary to be the user-description for the selected value
			titlePref.setSummary(sharedPreferences.getString(key, ""));
		}
		if (key.equals(COLOR_KEY)) {
			Preference colorPref = findPreference(key);
			// Set summary to be the user-description for the selected value
			colorPref.setSummary(sharedPreferences.getString(key, ""));
		}
		if (key.equals(CURRENCY_KEY)) {
			Preference currencyPref = findPreference(key);
			// Set summary to be the user-description for the selected value
			currencyPref.setSummary(sharedPreferences.getString(key, ""));
		}
	}

}
